package com.redrose.audioandvideo.audio;

import android.Manifest;
import android.media.AudioRecord;
import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.redrose.audioandvideo.R;
import com.redrose.audioandvideo.databinding.ActivityVideoBinding;
import com.redrose.audioandvideo.runnabletask.AudioRecordRunnable;
import com.redrose.audioandvideo.runnabletask.PlayPCMRunnable;
import com.redrose.audioandvideo.ui.activitys.BaseActivity;
import com.redrose.audioandvideo.utils.AudioRecordUtil;
import com.redrose.audioandvideo.utils.FileUtil;
import com.redrose.audioandvideo.utils.LogUtils;
import com.redrose.audioandvideo.utils.PathUtil;
import com.redrose.audioandvideo.utils.ThreadTask;
import com.redrose.audioandvideo.utils.ToastUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

@Route(path = "/activity/audio")
public class AudioActivity extends BaseActivity<ActivityVideoBinding> {
    String[] permission = new String[]{
            Manifest.permission.RECORD_AUDIO
    };
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMDD HH-mm-ss", Locale.getDefault());
    private boolean mIsAudio = false;
    private String mAudioName;
//    private PcmToWavUtil mPcmToWavUtil;
//    private AudioCapture audioCapture;
    /**
     * 声明recoordBufffer的大小字段
     */
    private int mRecordBufSize = 0;
    private AudioRecord mAudioRecord;

    @Override
    public void initData() {
//        audioCapture = new AudioCapture();
//        audioCapture.setOnAudioFrameCapturedListener(this);
//        mPcmToWavUtil = new PcmToWavUtil(AudioCapture.SAMPLE_RATE,AudioCapture.CHANNEL_CONFIG,AudioCapture.AUDIO_FORMAT);
    }

    @Override
    public int setLayout() {
        return R.layout.activity_video;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.setAudioHandler(new AudioHandler());
        if (!hasPermission(permission[0])) {
            requestPeermission(permission, 1000);
        }
        mBinding.chTime.setFormat("HH:MM:SS");
    }

    //     FileUtil.saveWAV(audioData,PathUtil.getAppAudioPath(),mAudioName);
    public class AudioHandler {
        public void startAudio() {
            if (mIsAudio) {
                mIsAudio = false;
                mBinding.btnVideo.setBackgroundResource(R.drawable.video_pause);
                stopRecord();
            } else {
                mIsAudio = true;
                mBinding.btnVideo.setBackgroundResource(R.drawable.video_start);
                mAudioName = sdf.format(new Date()) + ".pcm";
                initAudioRecord();
//                startRecording();
                ThreadTask.getInstance().executorOtherThread(new AudioRecordRunnable(mContext), ThreadTask.ThreadPeriod.PERIOD_HIGHT);
            }
        }

        public void switchPcmToWav(View view) {
            int id = view.getId();
            switch (id) {
                case R.id.btn_switch:
                    plawWavRecord();
                    break;
                case R.id.btn_play:
                    ThreadTask.getInstance().executorOtherThread(new PlayPCMRunnable(mContext), ThreadTask.ThreadPeriod.PERIOD_HIGHT);
                    break;

                default:
                    break;

            }
        }
    }

    /**
     * 初始化AudioRecord
     */
    private void initAudioRecord() {
        mRecordBufSize = AudioRecord.getMinBufferSize(AudioRecordUtil.AUDIO_SAMPLE_RATE, AudioRecordUtil.CHANNEL_CONFIG, AudioRecordUtil.AUDIO_FORMAT);
        mAudioRecord = AudioRecordUtil.getInstance().getAudioRecord(mRecordBufSize);
    }

    /**
     * 开始录音
     */
    private void startRecording() {
        ToastUtil.show(mContext.getApplicationContext(), "开始录音");
        LogUtils.d(TAG, "--开始录音--");
        ThreadTask.getInstance().executorOtherThread(new AudioRecordRunnable(mContext), ThreadTask.ThreadPeriod.PERIOD_HIGHT);
    }

    /**
     * pcm to wav
     */
    private void plawWavRecord() {
        PcmToWavUtil pcmToWavUtil = new PcmToWavUtil(AudioRecordUtil.AUDIO_SAMPLE_RATE, AudioRecordUtil.CHANNEL_CONFIG, AudioRecordUtil.AUDIO_FORMAT);
        String inFilename = PathUtil.getAppAudioPath() + "audio.pcm";
        File inFile = new File(inFilename);
        if (!inFile.exists()) {
            ToastUtil.show(mContext, "原始音频数据文件不存在");
            return;
        }
        pcmToWavUtil.pcmToWav(PathUtil.getAppAudioPath() + "audio.pcm", PathUtil.getAppAudioPath() + "audioWav.wav");
        ToastUtil.show(mContext.getApplicationContext(), "转换格式结束");
        LogUtils.d(TAG, "--转换格式结束--");
    }

    /**
     * 停止录音
     */
    private void stopRecord() {
        AudioRecordUtil.getInstance().setRecordStatues(false);
    }
}
